package com.devplatform.service.impl.points;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.devplatform.mapper.points.PointsMallMapper;
import com.devplatform.pojo.points.PointsMall;
import com.devplatform.service.points.PointsMallService;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * @author 86156
 * @description 针对表【points_mall】的数据库操作Service实现
 * @createDate 2025-09-28 14:44:33
 */
@Service
public class PointsMallServiceImpl extends ServiceImpl<PointsMallMapper, PointsMall>
        implements PointsMallService {

    @Resource
    private PointsMallMapper pointsMallMapper;


    @Override
    @Transactional(rollbackFor = Exception.class)
    //添加商品
    public void addGoods(PointsMall goods) {
        if (goods.getStock() == null) {
            goods.setStock(0);
        }
        if (goods.getStatus() == null) {
            goods.setStatus(1);
        } else if (goods.getStatus() != 0 && goods.getStatus() != 1) {
            throw new RuntimeException("状态值不合法（0=下架，1=上架）");
        }
        if (goods.getGoodsName() == null || goods.getPointsNeeded() == null || goods.getPointsNeeded() <= 0) {
            throw new RuntimeException("商品名称和所需积分不能为空且积分大于0");
        }
        pointsMallMapper.insert(goods);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    //编辑商品
    public void updateGoods(PointsMall goods) {
        // 校验状态只能是0或1（如果传入了状态）
        if (goods.getStatus() != null && goods.getStatus() != 0 && goods.getStatus() != 1) {
            throw new RuntimeException("状态值不合法（0=下架，1=上架）");
        }
        if (goods.getGoodsId() == null) {
            throw new RuntimeException("商品id不能为空");
        }
        if (goods.getGoodsName() == null || goods.getPointsNeeded() == null || goods.getPointsNeeded() <= 0) {
            throw new RuntimeException("商品名称和所需积分不能为空且积分大于0");
        }
        pointsMallMapper.updateById(goods);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void offGoods(Integer goodsId) {
        updateGoodsStatus(goodsId, 0, "下架");
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void onGoods(Integer goodsId) {
        updateGoodsStatus(goodsId, 1, "上架");
    }

    private void updateGoodsStatus(Integer goodsId, Integer targetStatus, String action) {
        if (goodsId == null) {
            throw new RuntimeException("商品id不能为空");
        }
        PointsMall goods = pointsMallMapper.selectById(goodsId);
        if (goods == null) {
            throw new RuntimeException("商品不存在，ID:" + goodsId);
        }
        // 避免重复操作
        if (goods.getStatus().equals(targetStatus)) {
            throw new RuntimeException("商品已是" + action + "状态");
        }
        goods.setStatus(targetStatus);
        pointsMallMapper.updateById(goods);
    }
    @Override
    public Page<PointsMall> getPointsMall(Integer userLevel, Integer pageNum, Integer pageSize) {
        if (userLevel == null) {
            throw new RuntimeException("用户等级不能为空");
        }
        if (pageNum == null || pageNum < 1) {
            pageNum = 1;
        }
        if (pageSize == null || pageSize < 1) {
            pageSize = 10;
        }

        Page<PointsMall> page = new Page<>(pageNum, pageSize);
        return pointsMallMapper.getPointsMall(page, userLevel);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void removeStock(PointsMall pointsMall) {
        if (pointsMall.getStock() <= 0) {
            throw new RuntimeException("库存不足");
        }

        pointsMall.setStock(pointsMall.getStock() - 1);
        baseMapper.updateById(pointsMall);
    }
}




